<?php
/**
 * Created by PhpStorm.
 * User: poptser
 * Email: poptser@foxmail.com
 * Date: 2016/8/19
 * Time: 19:37
 */

namespace SDXApi\Controller;
use Com\Jdpay\JdpayNotify;
use Think\Controller;
use Think\Log;
class JdpaynotifyController extends Controller
{
    /**
     *京东支付异步判断支付结果
     */
    public function jdpay_notifypay(){
        set_time_limit(0);
//        $this->write_log('jdpay begin: ',"jd_pay");
    	$jdpayNotify = new JdpayNotify();
        $verify_result = $jdpayNotify->verifyNotify();
//        $this->write_log('jdpay data: '.json_encode($verify_result),"jd_pay");
//        exit;
        if($verify_result) {//验证成功
            $trade_result = $verify_result['result'];
            $out_trade_no = $verify_result['tradeNum'];//商户订单号

            if($verify_result['status'] == '2' && $trade_result['desc'] == 'success') {
                $out_trade_db = explode("s",$out_trade_no);
                $total_fee = $verify_result['amount']/100;
                if($out_trade_db[1] == PAY_SURPLUS){
                    //充值回调修改
                    $update = D('Payment')->updata_recharg_log($out_trade_db[0]);    //更新支付状态
                    if($update){
                    	$verify_result['out_trade_no'] = $out_trade_db[0];
                    	D('Payment')->update_jdpay_log($verify_result,PAY_SURPLUS);
                        echo "success";
                    }else{
                        echo "fail";
                    }

                }elseif($out_trade_db[1] == PAY_ORDER){
                    $pay_order = D('Payment')->get_pay_log($out_trade_db[0],$out_trade_db[1]);
                    
                    if(floatval($total_fee)-floatval($pay_order["order_amount"])<0){
                        $surplus['user_id'] = $pay_order['user_id'];
                        $surplus['amount'] = $total_fee;
                        $surplus['process_type'] = 0;
                        $surplus['user_note'] = 0;
                        $surplus['payment'] = '京东支付';
                        $surplus['admin_note'] = '混合支付';
                        $log_id = D("Payment")->add_recharge_log($surplus,$total_fee);
                        
                        $update = D('Payment')->updata_recharg_log($log_id);    //更新支付状态
                        
                        if($update){
                            $verify_result['out_trade_no'] = $log_id;
                            D('Payment')->update_jdpay_log($verify_result,PAY_SURPLUS);
                            D("Payment")->tobalance_pay($out_trade_db[0]);
                            
                            echo "success";
                        }else{
                            echo "fail";
                        }
               
                    
                    }else{
                        $update = D('Payment')->order_paid($out_trade_db[0]);    //更新支付状态
                        //订单支付
                        if($update['pay_success']){
                            $verify_result['out_trade_no'] = $out_trade_db[0];
                            D('Payment')->update_jdpay_log($verify_result,PAY_ORDER);
                            echo "success";
                        }else{
                            echo "fail";
                        }
                    }


                }
            }
            //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
//             echo "success";		//请不要修改或删除
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
        else {
            //验证失败
            echo "fail";
            //调试用，写文本函数记录程序运行情况是否正常
            //logResult("这里写入想要调试的代码变量值，或其他运行的结果记录");
        }
    }
    
    public function write_log($message,$filename){
    	$log = new Log();
    	$destination = C('LOG_PATH').date('y_m_d').'_'.$filename.'.log';
    
    	$log->write($message,'DEBUG','',$destination);
    }
}